﻿using System;

namespace Test1
{
    class Program
    {
        private static double num1 = 0, num2 = 0, num3 = 0;
        private static string str = null;
        static void menu()
        {
            // Console.Clear();
            Console.WriteLine("====c#第一个控制台程序====");
            Console.WriteLine("       + -- 加法"          );
            Console.WriteLine("       - -- 减法"          );
            Console.WriteLine("       * -- 乘法"          );
            Console.WriteLine("       / -- 除法"          );
            Console.WriteLine("       % -- 求余"          );
            Console.WriteLine("       0 -- 退出"          );
        }
        static void parameter1()
        {
            try//try -catch 语句
            {
                Console.WriteLine("请输入第一个参数:");
                num1 = double.Parse(Console.ReadLine());//将数字的字符串表示形式转换为它的等效双精度浮点数
            }                                           //只能转换数字类型的字符串
            catch (Exception e)
            {
                Console.WriteLine("您输入的有错，请重新输入.");
                parameter1();
            }
        }
        static void parameter2()
        {
            try
            {
                Console.WriteLine("请输入第二个参数:");
                num2 = double.Parse(Console.ReadLine());
            }
            catch (Exception e)
            {
                Console.WriteLine("您输入的有错，请重新输入.");
                parameter2();
            }
        }
        static void caculate(string code)
        {
            switch (code)
            {
                case "+":
                    num3 = num1 + num2;
                    break;
                case "-":
                    num3 = num1 - num2;
                    break;
                case "/":
                    try
                    {
                        if (num2 == 0)
                        {
                            throw new DivideByZeroException();
                        }
                        else
                        {
                            num3 = num1 / num2;
                        }
                    }
                    catch (DivideByZeroException)
                    {
                        Console.WriteLine("除数不能为零");
                        Console.WriteLine("按回车继续......");
                        Console.ReadLine();
                        doit();
                    }
                    break;
                case "*":
                    num3 = num1 * num2;
                    break;

                case "%":
                    try
                    {
                        if (num2 == 0)
                        {
                            throw new DivideByZeroException();
                        }
                        else
                        {
                            num3 = num1 % num2;
                        }
                    }
                    catch (DivideByZeroException)
                    {
                        Console.WriteLine("除数不能为零");
                        Console.WriteLine("按回车继续......");
                        Console.ReadLine();
                        doit();
                    }
                    break;
            }
        }
        static void doit()
        {
            while (true)
            {
                menu();
                Console.WriteLine("请输入一个运算符（输入0退出）");
                str = Console.ReadLine();
                if (str == "0")
                {
                    Environment.Exit(0);//强制退出主窗口方法
                }
                try
                {
                    if (str == "+" || str == "-" || str == "/" || str == "*" || str == "%")
                    {
                        parameter1();
                        parameter2();
                        caculate(str);
                        Console.WriteLine("结果是:");
                        Console.WriteLine("{0}{1} {2} = {3}", num1, str, num2, num3);
                    }
                    else
                    {
                        throw new FormatException();
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine("你输入的有错请重新输入");
                }
                finally
                {
                    Console.WriteLine("按回车继续......");
                    Console.ReadLine();
                    doit();
                }
            }
        }
        static void Main(string[] args)
        {
            doit();
        }
    }
}
